//
//  CZNetworkTools.swift
//  WH01
//
//  Created by Apple on 16/7/8.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
import AFNetworking


//  请求方式

enum RequestType: Int {
    case GET
    case POST
    
}

class CZNetworkTools: AFHTTPSessionManager  {
    
    typealias callBackType = (response: AnyObject?, error: NSError?)->()

    //  单例全局访问点
    static let sharedTools: CZNetworkTools = {
        let tool = CZNetworkTools()
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tool
    }()
    
    //  封装get/post网络请求
    func request(type: RequestType, url: String, params: AnyObject?, callBack: callBackType) {
        
        //  封装方式2
        let successClosure = { (dataTask: NSURLSessionDataTask, response: AnyObject?) -> Void in
            callBack(response: response, error: nil)
        }
        
        let failureClosure = { (dataTask: NSURLSessionDataTask?, error: NSError) -> Void in
            callBack(response: nil, error: error)
        }
        
        if type == .GET {
            GET(url, parameters: params, progress: nil, success: successClosure, failure: failureClosure)
            
        } else {
            POST(url, parameters: params, progress: nil, success: successClosure, failure: failureClosure)
        }
        
        //  封装方式1
        
//        if type == .GET {
//            GET(url, parameters: params, progress: nil, success: { (_, response) -> Void in
//                    callBack(response: response, error: nil)
//                }, failure: { (_, error) -> Void in
//                    callBack(response: nil, error: error)
//            })
//            
//        } else {
//            POST(url, parameters: params, progress: nil, success: { (_, response) -> Void in
//                    callBack(response: response, error: nil)
//                }, failure: { (_, error) -> Void in
//                    callBack(response: nil, error: error)
//            })
//        }
        
        
    }
    
    
    
}


//  OAuth登录相关接口
extension CZNetworkTools {
    //  根据code获取accesstoken
    func requestAccessToken(code: String, callBack: callBackType) {
        //  url接口地址
        let url = "https://api.weibo.com/oauth2/access_token"
        //  准备参数
        let params = [
            "client_id": WeiBoAppKey,
            "client_secret": WeiBoAppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": WeiBoredirect_Uri
        ]
        
        request(.POST, url: url, params: params, callBack: callBack)
    }
    
    //  获取用户信息
    func requestUserInfo(userAccount: CZUserAccount, callBack: callBackType) {
        
        //  接口地址
        let url = "https://api.weibo.com/2/users/show.json"
        let params = [
            "access_token": userAccount.access_token!,
            "uid": "\(userAccount.uid)"
        ]
        request(.GET, url: url, params: params, callBack: callBack)
    }
}














